package com.sjj.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.sjj.springcloud.pojo.Dept;
import com.sjj.springcloud.service.DeptService;
import com.sjj.springcloud.service.Impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

//提供Restful服务!
@RestController
public class DeptController {

    @Resource
    private DeptService deptService;

    @GetMapping(value = "dept/get/{id}",produces = ("application/json"))
    @HystrixCommand(fallbackMethod = "mystrixGet")
    public Dept get(@PathVariable("id") Long id){
        Dept dept = deptService.get(id);
        if(dept==null)
            throw new RuntimeException("不存在该用户");
        return dept;
    }

    //备选方法
    public Dept mystrixGet(@PathVariable("id")Long id){
        return new Dept()
                .setDeptno(id)
                .setDname("没有对应信息")
                .setDb_source("没有对应数据库");
    }
}
